import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @ClassName: HttpTest
 * @Description: http请求
 * @Author: zyy
 * @Date: 2020/05/12 13:33
 * @Version: 1.0
 */
public class HttpTest {

    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>(3);
        map.put("policyNo", "12300612545656");
        map.put("serialNo", "1234560001");
        String paraString = mapToXml(map);
        System.out.println(paraString);

        String resultString = doPost("http://www.baidu.com", paraString);
        System.out.println(resultString);



    }

    public static String doPost(String url, String paraString) {
        //创建httpClient实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建httpPost连接实例
        HttpPost httpPost = new HttpPost(url);
        //配置请求参数实例
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(35000)
                .setConnectionRequestTimeout(35000)
                .setSocketTimeout(60000)
                .build();
        //为httpPost实例设置配置
        httpPost.setConfig(requestConfig);
        //设置请求头
        httpPost.setHeader("Content-Type", "application/xml;charset=UTF-8");
        httpPost.setHeader("Accept", "application/json;charset=UTF-8");
        //封装post请求参数
        httpPost.setEntity(new StringEntity(paraString, "UTF-8"));

        try {
            //httpClient执行post请求
            CloseableHttpResponse execute = httpClient.execute(httpPost);
            //从响应对象中获取响应内容
            HttpEntity entity = execute.getEntity();
            return EntityUtils.toString(entity);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 只能是单层级
     * @param map
     * @return
     */
    private static String mapToXml(Map<String, String> map) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        stringBuilder.append("<root>");

        if (map != null && !map.isEmpty()) {
            Set<Map.Entry<String, String>> set = map.entrySet();
            Iterator<Map.Entry<String, String>> iterator = set.iterator();
            while (iterator.hasNext()) {
                Map.Entry<String, String> entry = iterator.next();
                String key = entry.getKey();
                String value = entry.getValue();
                stringBuilder.append("<").append(key).append(">")
                        .append(value)
                        .append("</").append(key).append(">");
            }
        }
        stringBuilder.append("</root>");
        return stringBuilder.toString();
    }
}
